package com.yc.demo;

import com.yc.jdbc.d0605.JdbcTemplate;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

// loadOnStartup 在服务器启动时创建并初始化
@WebServlet(name = "QueryUserServlet", value = "/queryUser.s",
            loadOnStartup = 1)
public class QueryUserServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {
        // 初始化方法, 在构造函数执行完之后调用
        System.out.println("======= QueryUserServlet init ===========");
    }

    public void destroy(){
        System.out.println("======= QueryUserServlet destroy ===========");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("======= QueryUserServlet service ===========");
        super.service(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final List<Map<String, Object>> list = JdbcTemplate.select("select * from user");
        response.setContentType("text/html;charset=utf-8");
        final PrintWriter out = response.getWriter();
        out.append("<table>")
                .append("<tr><td>账号</d><td>密码</d><td>电话</td><tr>");
        for (Map<String, Object> user : list) {
            out.append("<tr><td>" +
                    user.get("name") + "</td><td>" +
                    user.get("pwd") + "</td><td>" +
                    user.get("phone") + "</td><tr>");
        }
        out.append("</table>");
    }

}
